/**
 * @Classname BinarySearrch 01 二分查找
 * @Description 有递归和非递归两种, 只适合有序数列中取值→先排序
 * @Date 2021/12/23 13:33
 * @Created K024851
 */
public class BinarySearrchNoRecur {
    public static void main(String[] args) {
        int[] arr = {1,3,8,10,11,67,100};
        int index = binarySearch(arr,100);
        System.out.println(index);
    }
    //二分查找非递归实现
    public  static int  binarySearch(int[] arr, int target){
        int left = 0;
        int right = arr.length-1;
        while (left<=right){
            int mid = (left+right)/2;
            if (arr[mid] ==target){
                return mid;
            }else if (arr[mid]>target){
                right = mid-1;
            }else {
                left = mid+1;
            }
        }
             return -1;
    }
}
